package org.test.once.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.test.once.manager.UserManager;
import org.test.once.model.User;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("GB2312");

		UserManager userManager = new UserManager();
		String command = req.getParameter("command");
		/**
		 * 分发控制
		 */
		if ("login".equals(command)) {
			String account = req.getParameter("account");
			String password = req.getParameter("password");
			User user = userManager.login(account, password);
			if (user != null) {
				// 将用户添加到session中
				req.getSession().setAttribute("login", user);
				List<User> users = userManager.searchUsers();
				req.setAttribute("users", users);
				req.getRequestDispatcher("user_list.jsp").forward(req, resp);
			} else {
				resp.sendRedirect("index.jsp");
			}
		} 

		if ("addUser".equals(command)) {
				String account = req.getParameter("account");
				String password = req.getParameter("password");
				String username = req.getParameter("username");
				User user = new User();
				user.setAccount(account);
				user.setPassword(password);
				user.setUserName(username);
				userManager.addUser(user);
				req.getRequestDispatcher("common/pub_add_success.jsp").forward(
						req, resp);
			} else if ("delUser".equals(command)) {
				String userId = req.getParameter("userId");
				int id = Integer.parseInt(userId);
				userManager.delUser(id);
				req.getRequestDispatcher("common/pub_del_success.jsp").forward(
						req, resp);
			} else if("modifyUser".equals(command)) {
				
				String userId = req.getParameter("role_id");
				int roleid = Integer.parseInt(userId);
				//System.out.println(roleid);
				String updateid = req.getParameter("userid");
				int uid = Integer.parseInt(updateid);
				//System.out.println(uid);
				userManager.updateUserRole(roleid, uid);
				req.getRequestDispatcher("common/pub_update_success.jsp").forward(req, resp);
			} else if("updateUser".equals(command)) {
				
				String account = req.getParameter("account");
				String password = req.getParameter("password");
				String username = req.getParameter("username");
				String userId = req.getParameter("id");
				
				int id = Integer.parseInt(userId);
				User user = new User();
				user.setId(id);
				user.setAccount(account);
				user.setPassword(password);
				user.setUserName(username);
				userManager.updateUser(user);
				req.getRequestDispatcher("common/pub_update_success.jsp").forward(
						req, resp);
			} else {
				List<User> users = userManager.searchUsers();
				req.setAttribute("users", users);
				req.getRequestDispatcher("user_list.jsp").forward(req, resp);
			}

	}
}
